﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.OA.Interface.CRM;
using NetDon.Utilities;

namespace NetDon.OA.Portal.CRM
{
    [Navigation("客户管理", "客户信息管理")]
    public partial class CustomerManage : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageQueryAdapter1.PerformQuery();
            }
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var criteria = new CustomerInfoSearchCriteria
            {
                 CustomerCode = txtCustomCode.Text.Trim(),
                 CustomerName = txtCustomName.Text.Trim(),
                 ContactName = txtContactName.Text.Trim(),
                 AreaId = CurrentApplicationContext.CompanyId
            };

            e.Condition = criteria;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var service = ServiceContainer.GetService<ICustomerInfoService>();
            e.RecordCount = service.GetCustomerInfoCount((CustomerInfoSearchCriteria)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            var service = ServiceContainer.GetService<ICustomerInfoService>();
            e.DataSource = service.SearchCustomerInfoDatasByCondition((CustomerInfoSearchCriteria)e.Condition, gridCustomerInfoList.SortExpression,
                                                      gridCustomerInfoList.SortDirection, e.PageSize, e.PageIndex);
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }
        

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void btnDeleteDemand_Click(object sender, EventArgs e)
        {
            var service = ServiceContainer.GetService<ICustomerInfoService>();
            var customerId = fieldCustomerId.Value.ToGuid();
            //if (service.ValidateCustomerReferenced(customerId))
            //{
            //    Alert("此客户有关联数据，不能删除！");
            //    return;
            //}

            bool result = service.RemoveCustomerInfo(customerId);

            if (result)
            {
                PageQueryAdapter1.PerformQuery();
                Alert("删除客户信息成功!");
            }
            else
            {
                Alert("删除客户信息失败！");
            }
        }

        

        

        

        

        

        

        

       
    }
}